package fun.yao.common.monitor.info;

import fun.yao.common.Constant;
import fun.yao.common.msg.HashtableMsg;
import fun.yao.common.nio.GessSocketChannel;
import fun.yao.common.uitls.LogUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;

/**
 * socket 信息
 * @author longjinglai
 *
 */
public class SocketInfo implements IMonitorInfo {

    /**socket 明细*/
    private List<SocketDetail> alSocketDetail = new ArrayList<SocketDetail>();

    public SocketInfo()
    {

    }

    public SocketInfo(HashtableMsg htm)
    {
        for(int i=0;i<htm.size();i++)
        {
            Hashtable<String, String> mapSocketInfo = htm.get(i);
            if(mapSocketInfo!=null)
            {
                SocketDetail detail = new SocketDetail(mapSocketInfo.get("socket_name"),Integer.parseInt(mapSocketInfo.get("port")),
                        Integer.parseInt(mapSocketInfo.get("local_port")),
                        mapSocketInfo.get("host"),
                        Double.parseDouble(mapSocketInfo.get("use_rate")),
                        Double.parseDouble(mapSocketInfo.get("write_use_rate")),
                        Double.parseDouble(mapSocketInfo.get("read_use_rate")),
                        mapSocketInfo.get("last_send_time"),
                        mapSocketInfo.get("last_recv_time"));
                this.alSocketDetail.add(detail);
            }
        }
    }

    /**
     * 转换HashtableMsg
     * @return
     */
    public HashtableMsg toHashtableMsg()
    {
        HashtableMsg htm = new HashtableMsg();
        for(int i=0;i<this.getSocketDetail().size();i++)
        {
            Hashtable<String, String> mapSocketInfo = new Hashtable<String, String>();
            SocketDetail detail = this.getSocketDetail().get(i);
            mapSocketInfo.put("socket_name", detail.getSocketName());
            mapSocketInfo.put("port", detail.getPort()+"");
            mapSocketInfo.put("local_port", detail.getLocalPort()+"");
            mapSocketInfo.put("host", detail.getHostAddress());
            mapSocketInfo.put("use_rate", detail.getUseRate()+"");
            mapSocketInfo.put("write_use_rate", detail.getWriteUseRate()+"");
            mapSocketInfo.put("read_use_rate", detail.getReadUseRate()+"");
            mapSocketInfo.put("last_send_time", detail.getLastSendTime()+"");
            mapSocketInfo.put("last_recv_time", detail.getLastRecvTime()+"");
            htm.add(mapSocketInfo);
        }
        return htm;
    }

    public List<SocketDetail> getSocketDetail()
    {
        return this.alSocketDetail;
    }

    @Override
    public String toString() {
        return "SocketInfo [alSocketDetail=" + alSocketDetail + "]";
    }


    /**
     * socket 明细
     * @author longjinglai
     *
     */
    public class SocketDetail
    {
        private String socketName = "";

        /**端口*/
        private int port ;

        /**本地端口*/
        private int localPort;

        /**地址*/
        private String hostAddress = "";

        /**5分钟 平均流量 字节/秒*/
        private double useRate ;

        /**5分钟 平均写流量 字节/秒*/
        private double writeUseRate;

        /**5分钟 平均读流量 字节/秒*/
        private double readUseRate;

        /**最后发送时间*/
        private String lastSendTime ;

        /**最后接收时间*/
        private String lastRecvTime ;

        public SocketDetail(GessSocketChannel socket)
        {
            this.port = socket.getSocketChannel().socket().getPort();
            this.localPort = socket.getSocketChannel().socket().getLocalPort();
            this.hostAddress = socket.getSocketChannel().socket().getLocalAddress().getHostAddress();
            this.useRate = socket.getUseRate();
            this.writeUseRate = socket.getWriteUseRate();
            this.readUseRate = socket.getReadUseRate();
            this.socketName = socket.getSocketChannel().socket().toString();

            SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
            lastSendTime = format.format(new Date(socket.getLastSendDataTime()));
            lastRecvTime = format.format(new Date(socket.getLastRecvDataTime()));

        }

        public SocketDetail(String socketName, int port, int localPort, String hostAddress, double useRate, double writeUseRate, double readUseRate, String lastSendTime, String lastRecvTime)
        {
            this.port = port;
            this.localPort = localPort;
            this.hostAddress = hostAddress;
            this.useRate = useRate;
            this.writeUseRate = writeUseRate;
            this.readUseRate = readUseRate;
            this.lastSendTime = lastSendTime;
            this.lastRecvTime = lastRecvTime;
            this.socketName = socketName;
        }

        public int getPort() {
            return port;
        }

        public int getLocalPort() {
            return localPort;
        }

        public String getHostAddress() {
            return hostAddress;
        }

        public double getUseRate() {
            return useRate;
        }

        public double getWriteUseRate() {
            return writeUseRate;
        }

        public double getReadUseRate() {
            return readUseRate;
        }

        public String getLastSendTime() {
            return lastSendTime;
        }

        public String getLastRecvTime() {
            return lastRecvTime;
        }

        public String getSocketName() {
            return socketName;
        }

        @Override
        public String toString() {
            return "SocketDetail [socketName=" + socketName + ",port=" + port + ", localPort=" + localPort
                    + ", hostAddress=" + hostAddress + ", lastRecvTime="
                    + lastRecvTime + ", lastSendTime=" + lastSendTime
                    + ", useRate=" + useRate + ", readUseRate=" + readUseRate
                    + ", writeUseRate=" + writeUseRate + "]";
        }
    }


    public void print(StringBuilder sbOut) {
        try
        {
            for(int i=0;i<this.getSocketDetail().size();i++)
            {
                SocketDetail detailInfo = this.getSocketDetail().get(i);
                sbOut.append("第" + i + "个Socket["+detailInfo.getHostAddress()+"]使用情况\n");
                sbOut.append("地址："+detailInfo.getHostAddress()+"，"+
                        "端口："+detailInfo.getPort()+"，" +
                        "本地端口："+detailInfo.getLocalPort()+"，" +
                        "平均流量："+detailInfo.getUseRate()+"，" +
                        "平均写流量："+detailInfo.getWriteUseRate()+"，" +
                        "平均读流量："+detailInfo.getReadUseRate()+"，最后发送时间："+detailInfo.getLastSendTime()+"，最后接收时间："+detailInfo.getLastRecvTime()+"\n");
            }
        }catch (Exception e) {
            LogUtil.WriteLog(Constant.BADLY_ERROR, e);
        }
    }
}

